package com.pj.project.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pj.current.satoken.AuthConst;
import com.pj.project.model.mapper.SpGoodsSortMapper;
import com.pj.project.model.pojo.SpGoodsSort;
import com.pj.project.model.pojo.SpGoodsWarehouse;
import com.pj.project.model.vo.SpGoodsSortVO;
import com.pj.project.service.SpGoodsSortService;
import com.pj.utils.sg.AjaxJson;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * GoodsSortController
 *
 * @author hanWu
 * 2023-01-26 11:36
 */
@RestController
@RequestMapping("/goodsSort")
public class GoodsSortController {

    @Resource
    SpGoodsSortService spGoodsSortService;

    @Resource
    SpGoodsSortMapper spGoodsSortMapper;

    @GetMapping("/success")
    public AjaxJson success() {
        return AjaxJson.getSuccess();
    }

    /**
     * 查看所有仓库信息
     */
    @PostMapping("/list")
    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson list() {

        List<SpGoodsSortVO> spGoodsSortVOList = spGoodsSortService.listVo();
        return AjaxJson.getSuccessData(spGoodsSortVOList);
    }


    /**
     * 分类添加
     */
    @PostMapping
//    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson add(SpGoodsSort goodsSort) {
        goodsSort.setCreateTime(new Date());
        try {
            spGoodsSortService.save(goodsSort);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxJson.getError("添加失败,检查分类是否重复");
        }

        return AjaxJson.getSuccess("添加成功", goodsSort);

    }

    /**
     * 分类删除
     */
    @DeleteMapping
//    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson del(Integer id) {
        if (spGoodsSortService.del(id)) {
            return AjaxJson.getSuccess("删除成功");
        } else {
            return AjaxJson.getError("删除失败");
        }
    }


    @PostMapping("/update")
//    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson updateById(SpGoodsSort goodsSort) {
        int result = spGoodsSortMapper.updateById(goodsSort);
        if (result > 0) {
            return AjaxJson.getSuccess("修改成功",result);
        } else {
            return AjaxJson.getError("修改失败");
        }
    }

    /**
     * 根据id查询分类名字
     */
    @PostMapping("/selectById")
    //    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson selectName(Long id) {
        if (id != null) {
            String s = spGoodsSortMapper.selectByIdForName(id);
            return AjaxJson.getSuccess("查询成功",s);
        }else{
            return AjaxJson.getError("未传入id");
        }
    }


}
